package qe.spider.grab;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import qe.spider.vo.Stock;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;

@Slf4j
@Data
public class ParseThsStock {
    public static List<String> disks = List.of("D:\\Tools\\THSGeGuData\\", "X:\\Tools\\THSGeGuData\\", "Y:\\Tools\\THSGeGuData\\", "Z:\\Tools\\THSGeGuData\\");
    public static final String basePath = System.getProperty("ths_base_path", "D:\\Tools\\") + "THSGeGuData\\";

    private File excelFile;
    private Date grabTime;
    private Map<String, Stock> stocks = new HashMap<>();

    private List<String> lines = new ArrayList<>();
    private List<String> title = new ArrayList<>();
    private List<Integer> titleIdx = new ArrayList<>();
    private List<Stock> all = new ArrayList<>();

    public ParseThsStock(File excelFile) throws IOException {
        this.excelFile = excelFile;
        this.grabTime = parseGrabTime();
    }

    public ParseThsStock(String excelFilePath) throws IOException {
        this.excelFile = new File(excelFilePath);
        this.grabTime = parseGrabTime();
    }

    private Date parseGrabTime() {
        String time = this.excelFile.getName().split("\\.")[0];
        String date = this.excelFile.getParentFile().getName();
        return DateTime.of(date + " " + time, time.contains("_") ? "yyyy-MM-dd HHmmss_SSS" : "yyyy-MM-dd HHmmss");
    }

    public List<String> parseTitle() {
        this.lines = FileUtil.readLines(excelFile, Charset.forName("gb2312"));
        int i = 0;
        for (String t : lines.get(0).split("\t")) {
            title.add(t.trim());
            titleIdx.add(i);
            i++;
        }
        return title;
    }

    public List<Stock> parseAllLines() {
        if (title.isEmpty()) parseTitle();
        for (int i = 1; i < this.lines.size(); i++) {
            List<String> sd = new ArrayList<>();
            for (String t : lines.get(i).split("\t")) {
                sd.add(t.trim());
            }
            if (sd.size() == title.size()) {
                Stock s = parseLineStock(sd);
                all.add(s);
                stocks.put(s.getStock_code(), s);
            }
        }
        return all;
    }

    private Stock parseLineStock(List<String> items) {
        Stock s = new Stock();
        s.setGrabTime(this.grabTime);
        for (int i = 0; i < title.size(); i++) {
            String t = title.get(i);
            String d = items.get(i);//.replaceAll("-", "");
            if (d.isEmpty()) continue;
            try {
                if ("代码".equals(t)) {
                    if (d.startsWith("SZ")) s.setMarket("SZ");
                    else if (d.startsWith("SH")) s.setMarket("SH");
                    else s.setMarket("");
                    s.setStock_code(d.substring(2));
                } else if ("名称".equals(t)) s.setStock_name(d.replaceAll("-", ""));
                else if ("涨幅".equals(t) && NumberUtil.isNumber(d.replaceAll("%", ""))) s.setZhangfu(d);
                else if ("现价".equals(t) && NumberUtil.isNumber(d)) s.setJiage(d);
                else if (t.contains("最高价") && NumberUtil.isNumber(d)) {
                    s.setZuiGaoJia(new BigDecimal(d));
                    s.setHighPrice(new BigDecimal(d));
                } else if (t.contains("涨停价") && NumberUtil.isNumber(d)) s.setZhangTingJia(new BigDecimal(d));
                else if (t.contains("跌停价") && NumberUtil.isNumber(d)) s.setDieTingJia(new BigDecimal(d));
                else if (t.contains("流通市值") && NumberUtil.isNumber(d)) s.setShiJiLiuTongZhi(new BigDecimal(d));
                else if (t.contains("主力净额") && NumberUtil.isNumber(d)) s.setZiJinJingE(new BigDecimal(d));
                else if (t.contains("主力净量") && NumberUtil.isNumber(d))
                    s.setZhuLiJingLiang(new BigDecimal(d).doubleValue());
                else if (t.contains("涨速") && NumberUtil.isNumber(d.replaceAll("%", ""))) {
                    s.setZhangSu4min(new BigDecimal(d.replaceAll("%", "")));
                    s.setSpeed(s.getZhangSu4min());
                } else if ("现手".equals(t)) {
                    if (d.contains("↑")) s.setXianShouZhang(true);
                    else if (d.contains("↓")) s.setXianShouZhang(false);
                    String xs = d.replaceAll("↑", "").replaceAll("↓", "").replaceAll("--", "");
                    if (NumberUtil.isNumber(xs)) s.setXianShou(new BigDecimal(xs));
                } else if ("总金额".equals(t) && NumberUtil.isNumber(d)) s.setChengJiaoE(new BigDecimal(d));
                else if ("换手".equals(t) && NumberUtil.isNumber(d.replaceAll("%", ""))) {
                    s.setHuanShou(new BigDecimal(d.replaceAll("%", "")));
                    s.setShiJiHuanShouLv(new BigDecimal(d.replaceAll("%", "")));
                } else if ("量比".equals(t) && NumberUtil.isNumber(d)) s.setLiangBi(new BigDecimal(d));
                else if ("利空".equals(t)) s.setLiKong("有".equals(d.replaceAll("-", "")));
                else if ("利好".equals(t)) s.setLiHao("有".equals(d.replaceAll("-", "")));
                else if ("昨收".equals(t) && NumberUtil.isNumber(d)) s.setZuoShou(new BigDecimal(d));
                else if ("开盘".equals(t) && NumberUtil.isNumber(d)) s.setKaiPan(new BigDecimal(d));
                else if (t.contains("涨停次数")) {
                    if (d.equals("--")) s.setZhangTingCnt5(0);
                    if (NumberUtil.isNumber(d)) s.setZhangTingCnt5(new BigDecimal(d).intValue());
                } else if (t.contains("跌停次数")) {
                    if (d.equals("--")) s.setDieTingCnt5(0);
                    if (NumberUtil.isNumber(d)) s.setDieTingCnt5(new BigDecimal(d).intValue());
                } else if (t.contains("个股热度排名")) {
                    if (NumberUtil.isNumber(d.trim().replaceAll("-", ""))) {
                        s.setRenqiRanking(new BigDecimal(d.trim().replaceAll("-", "")).intValue());
                    } else {
                        s.setRenqiRanking(0);
                    }
                } else if (t.contains("人气数值")) {
                    d = d.trim().replaceAll("-", "");
                    if (d.isEmpty()) s.setRenQi(null);
                    else if (d.endsWith("万") && NumberUtil.isNumber(d.replaceAll("万", "")))
                        s.setRenQi(new BigDecimal(d.replaceAll("万", "")).multiply(new BigDecimal(10000)));
                    else if (d.endsWith("亿") && NumberUtil.isNumber(d.replaceAll("亿", "")))
                        s.setRenQi(new BigDecimal(d.replaceAll("亿", "")).multiply(new BigDecimal(100000000)));
                    else s.setRenQi(new BigDecimal(d));
                } else if (t.contains("所属行业")) s.setSuoShuHangYe(d.replaceAll("-", ""));
                else if (t.contains("所属概念")) s.setSuoShuGaiNian(d.replaceAll("-", ""));
//                else if (t.contains("特大单买入金额") && NumberUtil.isNumber(d)) s.setVeryBigBuy(new BigDecimal(d));
                else if (t.startsWith("特大单买入金额")) {
                    d = d.trim().replaceAll("-", "");
                    if (d.isEmpty()) s.setVeryBigBuy(null);
                    else if (d.endsWith("万") && NumberUtil.isNumber(d.replaceAll("万", "")))
                        s.setVeryBigBuy(new BigDecimal(d.replaceAll("万", "")).multiply(new BigDecimal(10000)));
                    else if (d.endsWith("亿") && NumberUtil.isNumber(d.replaceAll("亿", "")))
                        s.setVeryBigBuy(new BigDecimal(d.replaceAll("亿", "")).multiply(new BigDecimal(100000000)));
                    else s.setVeryBigBuy(new BigDecimal(d));
                } else if (t.startsWith("大单买入金额")) {
                    d = d.trim().replaceAll("-", "");
                    if (d.isEmpty()) s.setBigBuy(null);
                    else if (d.endsWith("万") && NumberUtil.isNumber(d.replaceAll("万", "")))
                        s.setBigBuy(new BigDecimal(d.replaceAll("万", "")).multiply(new BigDecimal(10000)));
                    else if (d.endsWith("亿") && NumberUtil.isNumber(d.replaceAll("亿", "")))
                        s.setBigBuy(new BigDecimal(d.replaceAll("亿", "")).multiply(new BigDecimal(100000000)));
                    else s.setBigBuy(new BigDecimal(d));
                } else if (t.startsWith("特大单卖出金额")) {
                    d = d.trim().replaceAll("-", "");
                    if (d.isEmpty()) s.setVeryBigSell(null);
                    else if (d.endsWith("万") && NumberUtil.isNumber(d.replaceAll("万", "")))
                        s.setVeryBigSell(new BigDecimal(d.replaceAll("万", "")).multiply(new BigDecimal(10000)));
                    else if (d.endsWith("亿") && NumberUtil.isNumber(d.replaceAll("亿", "")))
                        s.setVeryBigSell(new BigDecimal(d.replaceAll("亿", "")).multiply(new BigDecimal(100000000)));
                    else s.setVeryBigSell(new BigDecimal(d));
                } else if (t.startsWith("大单卖出金额")) {
                    d = d.trim().replaceAll("-", "");
                    if (d.isEmpty()) s.setBigSell(null);
                    else if (d.endsWith("万") && NumberUtil.isNumber(d.replaceAll("万", "")))
                        s.setBigSell(new BigDecimal(d.replaceAll("万", "")).multiply(new BigDecimal(10000)));
                    else if (d.endsWith("亿") && NumberUtil.isNumber(d.replaceAll("亿", "")))
                        s.setBigSell(new BigDecimal(d.replaceAll("亿", "")).multiply(new BigDecimal(100000000)));
                    else s.setBigSell(new BigDecimal(d));
                } else if (t.contains("收盘价") && NumberUtil.isNumber(d)) s.setShoupanPrice(new BigDecimal(d));
                else if (t.contains("最高") && NumberUtil.isNumber(d)) {
                    s.setHighPrice(new BigDecimal(d));
                    s.setZuiGaoJia(new BigDecimal(d));
                } else if (t.contains("最低") && NumberUtil.isNumber(d)) s.setLowPrice(new BigDecimal(d));
                else if (t.contains("均价") && NumberUtil.isNumber(d)) s.setAvgPrice(new BigDecimal(d));
                else if (t.contains("开盘涨幅") && NumberUtil.isNumber(d.replaceAll("%", "")))
                    s.setKaipanRate(new BigDecimal(d.replaceAll("%", "")));
            } catch (Exception e) {
                log.error("解析同花顺数据异常----t:{}----d:{}", t, d, e);
            }
        }
        if (s.getBigBuy() != null && s.getVeryBigBuy() != null && s.getChengJiaoE() != null && s.getChengJiaoE().intValue() != 0) {
            s.setZiJinBuy(s.getBigBuy().add(s.getVeryBigBuy()));
            s.setBigBuyRate(s.getBigBuy().add(s.getVeryBigBuy()).divide(s.getChengJiaoE(), 4, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        if (s.getBigSell() != null && s.getVeryBigSell() != null && s.getChengJiaoE() != null && s.getChengJiaoE().intValue() != 0) {
            s.setZiJinSell(s.getBigSell().add(s.getVeryBigSell()));
            s.setBigSellRate(s.getBigSell().add(s.getVeryBigSell()).divide(s.getChengJiaoE(), 4, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        return s;
    }

    public static void main(String[] args) throws IOException {
        ParseThsStock parser = new ParseThsStock(new File("D:\\Tools\\THSGeGuData\\2024-07-19\\112640.xls"));
        parser.parseTitle();
        parser.parseAllLines();
        log.debug("{}", parser.all);
    }
}
